﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sern.Abstracts.Common;
using Sern.Common;
using Microsoft.Practices.Unity;
using Sern.ServiceImplementation;
using Sern.ServiceImplementation.MapReduce;
using LocalRegistrar = Sern.ServiceImplementation.LocalRegistrar;

namespace Sern.Test.ServiceImplementation.MapReduce
{
    [TestClass]
    public class MapReduceHostService_BinaryUploadAndHotload
    {
        [TestInitialize]
        public void Init()
        {
            FileStream fileStream = new FileStream(@"C:\SERN.src\Sern.Abstracts\Sern.Test\RemotelyUploadableTest.Main\RemotelyUploadableTest.Main\bin\TestClient.zip", FileMode.Open);
            BinaryReader fileReader = new BinaryReader(fileStream);
            byte[] clientBinaryPackage = fileReader.ReadBytes((int) fileStream.Length);

            ComponentLocator.Reset();
            ComponentLocator.Unity.RegisterInstance("ClientBinary",clientBinaryPackage);
            ComponentLocator.Unity.RegisterType<IMapReduceHost, MapReduceHostService>(new InjectionConstructor(""));
            ComponentLocator.Unity.RegisterInstance("ClientId", Guid.NewGuid());
            ComponentLocator.Unity.RegisterType<IBindingFactory, BindingFactory>();
            new LocalRegistrar().Register(ComponentLocator.Unity);
            //FileStream. read all bytes
        }

        [TestMethod]
        public void Test()
        {
            var host = ComponentLocator.Unity.Resolve<IMapReduceHost>();
            var clientId = ComponentLocator.Unity.Resolve<Guid>("ClientId");
            var clientBinary = ComponentLocator.Unity.Resolve<byte[]>("ClientBinary");
            host.UploadClient(clientId,clientBinary);
            host.Register(clientId, Guid.Empty, "");

            var fileCreated =
                new FileInfo(string.Format(@"C:\SERN.src\Sern.Abstracts\Sern.Test\bin\Debug\{0}\Debug\{1}",
                                           clientId.ToString("N"), clientId.ToString()));

            Assert.IsTrue(fileCreated.Exists);
        }
    }
}
